import 'package:azlistview/azlistview.dart';
import 'package:chat/controller/add_group_controller.dart';
import 'package:chat/model/friend_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

AddGroupController addGroupController = Get.find();

class AddGroupScreen extends StatelessWidget {
  const AddGroupScreen({super.key});

  Widget customSearch() {
    return Padding(
      padding: const EdgeInsets.all(16.0),
      child: TextField(
        decoration: InputDecoration(
          hintText: '搜索好友',
          hintStyle: TextStyle(color: Colors.grey[500]),
          prefixIcon: const Icon(Icons.search),
          border: OutlineInputBorder(
            borderRadius: BorderRadius.circular(10),
            borderSide: BorderSide.none,
          ),
          filled: true,
          fillColor: Color(0XFFF2F3F7),
          contentPadding: const EdgeInsets.symmetric(vertical: 0),
        ),
        onChanged: (value) {
          //controller.searchGroups(value);
        },
      ),
    );
  }

  Widget customGroup() {
    return InkWell(
      onTap: () {
        Get.toNamed('/groupListScreen');
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
        child: Row(
          children: [
            Container(
              width: 36,
              height: 36,
              decoration: BoxDecoration(
                shape: BoxShape.rectangle,
                borderRadius: BorderRadius.circular(4.0),
              ),
              child: Image(image: AssetImage('assets/images/group.png')),
            ),
            SizedBox(width: 16),
            Expanded(
              child: Text('群聊', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      resizeToAvoidBottomInset: false,
      backgroundColor: Colors.white,
      appBar: AppBar(
        backgroundColor: Colors.white,
        systemOverlayStyle: SystemUiOverlayStyle(
          statusBarColor: Colors.white,
          statusBarIconBrightness: Brightness.dark,
        ),
        elevation: 0,
        title: Text(
          '好友',
          style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold, color: Colors.black),
        ),
        centerTitle: true,
        leading: IconButton(
          icon: Icon(Icons.arrow_back, color: Colors.black),
          onPressed: () => Get.back(),
        ),
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 16),
            child: InkWell(
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 16, vertical: 8),
                decoration: BoxDecoration(
                  color: Colors.black,
                  borderRadius: BorderRadius.circular(50),
                ),
                child: Text('确定', style: TextStyle(color: Colors.white, fontSize: 16)),
              ),
              onTap: () {
                Get.back(result: addGroupController.selectedFriends);
              },
            ),
          ),
        ],
      ),
      body: Obx(
        () => Column(
          children: [
            Expanded(
              child: AzListView(
                data: addGroupController.friendList,
                itemCount: addGroupController.friendList.length,
                itemBuilder: (BuildContext context, int index) {
                  if (index == 0) {
                    return customSearch();
                  } else {
                    Friend friend = addGroupController.friendList[index];
                    return Container(
                      padding: EdgeInsets.symmetric(vertical: 8, horizontal: 16),
                      child: Row(
                        children: [
                          Obx(
                            () => Checkbox(
                              shape: CircleBorder(), // 圆形样式
                              activeColor: Color(0xFFE1F166), // 选中时的填充颜色
                              checkColor: Colors.black, // 选中时的对勾颜色
                              side: BorderSide(color: Colors.grey[400]!),
                              value: addGroupController.selectedFriends.contains(friend),
                              onChanged: (value) {
                                if (value == true) {
                                  print('选中好友：${friend.name}');
                                  addGroupController.selectedFriends.add(friend);
                                } else {
                                  addGroupController.selectedFriends.remove(friend);
                                }
                              },
                            ),
                          ),
                          SizedBox(width: 8),
                          Container(
                            width: 36,
                            height: 36,
                            decoration: BoxDecoration(
                              shape: BoxShape.rectangle,
                              borderRadius: BorderRadius.circular(4.0),
                            ),
                            child: Image(
                              image: AssetImage('assets/images/zh-CN-XiaoqiuNeural.png'),
                            ),
                          ),
                          SizedBox(width: 16),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(
                                  friend.name,
                                  style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
                                ),
                                Text(
                                  friend.subTitle ?? "",
                                  style: TextStyle(fontSize: 10, color: Colors.grey),
                                ),
                              ],
                            ),
                          ),
                        ],
                      ),
                    );
                  }
                },
                physics: BouncingScrollPhysics(),
                susItemBuilder: (BuildContext context, int index) {
                  Friend friend = addGroupController.friendList[index];
                  if ('↑' == friend.getSuspensionTag()) {
                    return Container();
                  }
                  return Container(
                    height: 40,
                    width: MediaQuery.of(context).size.width,
                    padding: EdgeInsets.only(left: 16.0),
                    color: Color(0XFFF2F3F7),
                    alignment: Alignment.centerLeft,
                    child: Text(
                      friend.getSuspensionTag(),
                      softWrap: false,
                      style: TextStyle(fontSize: 14.0, color: Color(0xFF666666)),
                    ),
                  );
                },
                indexBarData: kIndexBarData,
                indexBarOptions: IndexBarOptions(
                  needRebuild: true,
                  ignoreDragCancel: true,
                  downTextStyle: TextStyle(fontSize: 12, color: Colors.white),
                  downItemDecoration: BoxDecoration(shape: BoxShape.circle, color: Colors.green),
                  indexHintWidth: 120 / 2,
                  indexHintHeight: 100 / 2,
                  indexHintDecoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage('assets/images/ic_index_bar_bubble_gray.png'),
                      fit: BoxFit.contain,
                    ),
                  ),
                  indexHintAlignment: Alignment.centerRight,
                  indexHintChildAlignment: Alignment(-0.25, 0.0),
                  indexHintOffset: Offset(-20, 0),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
